<?php

class NepogodaWeather
{
	protected $_url = '';
	protected $_urlPattern   = 'http://www.nepogoda.ru/asia/israel/%place%/forecast.xml';
	protected $_placePattern = '%place%';
	
	private $_data  = array();
	private $_place = null;
	
	
	public function __construct($place = null)
	{
		if ($place) {
			$this->setPlace($place);
		}
	}
	
	public function setPlace($place)
	{
		$this->_url = str_replace($this->_placePattern, $place, $this->_urlPattern);
		
		return $this;
	}
	
	public function parseXml()
	{
		$xmlContents = file_get_contents($this->_url);
		
		if (!$xmlContents) { 
			throw new Exception('Error loading url: ' . $url);
		}
		
		$sxe = new SimpleXMLElement($xmlContents);
		
		$elements = $sxe->xpath('/weather/point/timestep');
		
		$this->_data = array();
		
		if (count($elements) > 0) {
			foreach($elements as $elem) {
				$date = date('d-m-Y', strval($elem->timestamp));
				
				if (!array_key_exists($date, $this->_data)) {
					$this->_data[$date] = array(); 
				}
				
				$this->_data[$date][] = (array)$elem;
			}
		} else {
			throw new Exception('Something incorrect: count of timestep in result is null... may be Doomsday ? :)');
		}
		
		return $this;
	}
	
	public function getSimpleInformation()
	{
		$arrs = current($this->_data);
		
		if (count($arrs) > 0) {
			$arr = $arrs[0];
			
			return array(
				'temperature'         => $arr['temperature'],
				'temperatureInterval' => $arr['temperature_interval'],
				'description' => $arr['description'],
				'pressure' => $arr['pressure'],
				'humidity' => $arr['humidity'],
				'cloud'    => $arr['cloud_text'],
				'precipitation' => $arr['precipitation_text']
			);
		}
		
		return false;
	}
}